package com.activity.gaswatchermtl.dialog.expendablelist;

import java.util.ArrayList;

import com.activity.gaswatchermtl.R;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.TextView;

public class ExpListAdapter extends BaseExpandableListAdapter {
	
	private Context context_;
	private ArrayList<ExpListGroup> groups_;
	
	public ExpListAdapter(Context context, ArrayList<ExpListGroup> groups) {
		this.context_ = context;
		this.groups_ = groups;
	}

	public void addItem(ExpListChild item, ExpListGroup group) {
		if (!groups_.contains(group)) {
			groups_.add(group);
		}
		int index = groups_.indexOf(group);
		ArrayList<ExpListChild> ch = groups_.get(index).getItems();
		ch.add(item);
		groups_.get(index).setItems(ch);
	}
	
	public Object getChild(int groupPosition, int childPosition) {
		ArrayList<ExpListChild> chList = groups_.get(groupPosition).getItems();
		return chList.get(childPosition);
	}
	
	public Object getChildByTag(String tag) {
		for (ExpListGroup group : groups_) {
			ArrayList<ExpListChild> items = group.getItems();
			for (ExpListChild tmpItem : items) {
				if (tmpItem.getTag().equals(tag)) {
					return tmpItem;
				}
			}
		}
		
		return null;
	}

	public long getChildId(int groupPosition, int childPosition) {
		return childPosition;
	}

	public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View view,
			ViewGroup parent) {
		ExpListChild child = (ExpListChild) getChild(groupPosition, childPosition);
		if (view == null) {
			LayoutInflater infalInflater = (LayoutInflater) context_.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			view = infalInflater.inflate(R.layout.explist_child_item, null);
		}
		CheckBox checkBox = (CheckBox) view.findViewById(R.id.explist_child_check_box);
		checkBox.setOnCheckedChangeListener(new checkListener());
		checkBox.setText(child.getName().toString());
		checkBox.setTag(child.getTag());
		checkBox.setChecked(child.isChecked());
		return view;
	}

	public int getChildrenCount(int groupPosition) {
		ArrayList<ExpListChild> chList = groups_.get(groupPosition).getItems();
		return chList.size();
	}

	public Object getGroup(int groupPosition) {
		return groups_.get(groupPosition);
	}

	public int getGroupCount() {
		return groups_.size();
	}

	public long getGroupId(int groupPosition) {
		return groupPosition;
	}

	public View getGroupView(int groupPosition, boolean isLastChild, View view, ViewGroup parent) {
		ExpListGroup group = (ExpListGroup) getGroup(groupPosition);
		if (view == null) {
			LayoutInflater inf = (LayoutInflater) context_.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			view = inf.inflate(R.layout.explist_group_item, null);
		}
		TextView tv = (TextView) view.findViewById(R.id.explist_group_text_view);
		tv.setText(group.getName());
		return view;
	}

	public boolean hasStableIds() {
		return true;
	}

	public boolean isChildSelectable(int arg0, int arg1) {
		return true;
	}
	
	private class checkListener implements OnCheckedChangeListener {

		@Override
		public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
			String tag = (String) buttonView.getTag();
			ExpListChild child = (ExpListChild) getChildByTag(tag);
			child.setChecked(isChecked);
		}
		
	}
}
